package cn.ly.security.controller;

import cn.ly.security.entity.DeviceUserArea;
import cn.ly.security.entity.Role;
import cn.ly.security.entity.User;
import cn.ly.security.service.UserService;
import cn.ly.security.service.impl.RoleServiceImpl;
import cn.ly.security.service.impl.UserServiceImpl;
import cn.ly.security.util.DataPojo;
import cn.ly.security.util.Pagely;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/User")
@Api(tags="用户管理")
@CrossOrigin("*")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserServiceImpl userServiceimp;
    @Autowired
    RoleServiceImpl roleServiceimp;

    @GetMapping("/selectUserList")
    public DataPojo selectuserList(Pagely pagely) {
        int count = userService.count();
        Page<User> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        userPage.setTotal(count);
        List<User> userList = userService.selectUserList(pagely.getCurrent(), pagely.getSize());
        userPage.setRecords(userList);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(userPage);
        return new DataPojo("200","无异常",objects);
    }
    @GetMapping("/selectUserListByName")
    public DataPojo selectUserListByName(Pagely pagely, String uUsernmae) {
        Page<User> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        List<User> userList = userService.selectUserListByName(uUsernmae,pagely.getCurrent(), pagely.getSize());
        int count = userService.count();
        userPage.setRecords(userList);
        userPage.setTotal(count);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(userPage);
        return new DataPojo("200","无异常",objects);
    }
    @GetMapping("/selectUserListById")
    public DataPojo selectUserListByName(Pagely pagely,Integer id) {
        Page<User> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        List<User> userList = userService.selectUserListById(id,pagely.getCurrent(), pagely.getSize());
        userPage.setRecords(userList);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(userPage);
        return new DataPojo("200","无异常",objects);
    }

    @GetMapping("/selectByName")
    public DataPojo selectByName(String name, Pagely pagely) {
        Page<User> userPage = new Page<>(pagely.getCurrent(), pagely.getSize());
        QueryWrapper<User> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("name",name);
        Page<User> page = userService.page(userPage, objectQueryWrapper);
        int count = userService.count();
        userPage.setTotal(count);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(page);
        return new DataPojo("200","无异常",objects);
    }
    @PutMapping("/insertUser")
    public DataPojo insert(Role role,User user){
        user.setUAuthority(role.getRId());
        boolean save = userServiceimp.save(user);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(save);
        return new DataPojo("200","无异常",objects);
    }
    @PostMapping("/updateUser")
    public DataPojo updateUser(Role role,User user){
        user.setUAuthority(role.getRId());
        boolean b = userServiceimp.updateById(user);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(b);
        return new DataPojo("200","无异常",objects);
    }
    @DeleteMapping("/deleteSingle")
    public DataPojo deleteSingleUser(User user){
        boolean b = userServiceimp.removeById(user);
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(b);
        return new DataPojo("200","无异常",objects);
    }
    @DeleteMapping("/deleteMultiple")
    public DataPojo deleteMultipleUser(Integer [] uIds){
        boolean b=false;
        for(int uId:uIds) {
             b = userServiceimp.removeById(uId);
        }
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(b);
        return new DataPojo("200","无异常",objects);
    }
}
